<?php
/**
 * Description of IndexController
 *
 * @author Admin
 */
class Main_AccountController extends Zend_Controller_Action
{
    protected $_model;
    protected $_authService;

    public function init()
    {
        $layout = $this->_helper->layout();
        $layout->setLayout('main');

        $this->_model = new Main_Model_User();
        $this->_authService = new Main_Service_Authentication();
         // add forms
        $this->view->loginForm = $this->getLoginForm();
        $this->view->newuserForm = $this->getRegisterForm();
        $this->view->forgotForm = $this->getForgotPasswordForm();
    }

    public function indexAction()
    {
        // checking if user is already logged in
        if($this->_authService->getAuth()->hasIdentity())
                $this->_redirect('/dashboard');
    }

    public function loginAction()
    {
        // checking if user is already logged in
        if($this->_authService->getAuth()->hasIdentity())
                $this->_redirect('/dashboard');
        
        if ($this->getRequest()->isPost())
        {
            $request = $this->getRequest();
            // Validate
            $form = $this->_forms['login'];
            if (!$form->isValid($request->getPost()))
            {
                return $this->render('index');
            }
            $this->_login();
        }
        else
            return $this->render('index');
        
    }

    public function logoutAction()
    {
        $this->_authService->clear();
        $this->_redirect('/');
    }

    /*
     * performs the authentication & redirects user to dashboard
     */
    protected function _login()
    {
        $request = $this->getRequest();
        $form = $this->_forms['login'];
        $formValues = $form->getValues();
        if (false === $this->_authService->authenticate($formValues)) {
            $form->addError('Invalid');
            return $this->render('index');
        }
        
        $this->_redirect('/dashboard');

    }

    public function registerAction()
    {
        $requst_params = $this->getRequest()->getParams();
        $post=array();
        $post['FirstName'] = $requst_params['fname'];
        $post['LastName'] = $requst_params['lname'];
        $post['UserEmail'] = $requst_params['email'];
        $post['Gender'] = $requst_params['gender'];
        $post['password'] = $requst_params['password'];
                
        if($this->_model->createUser($post)){
            $this->_helper->FlashMessenger('')->addMessage('You are resgistered successfully. Please check your email!');
        }else{
            $this->_helper->FlashMessenger('')->addMessage('Registration failed ! Try again later.');
        }
        $this->_redirect('/login');
    }
    
    public function verifyAction(){
        $requst_params = $this->getRequest()->getParams();
        $verificationCode = $requst_params['VerificationCode'];
        $res = $this->_model->getUserByVerificationCode($verificationCode);
        
        $this->_model->verifyUser($res->toArray());
        $this->_helper->FlashMessenger('')->addMessage('Email has been verified. Please login to continue!');
        $this->_redirect('/login');
        
    }
    
    public function forgotAction()
    {
        $requst_params = $this->getRequest()->getParams();
        $email = $requst_params['ForgotUserEmail'];
        $res = $this->_model->getUserByEmail($email);
        $res = $res->toArray();
        $pwd = $res['password'];
        //send_email();
        
        $this->_helper->FlashMessenger('')->addMessage(array('Please check your email. Password has been sent!'));
        $this->_redirect('/login');
    }

    public function getLoginForm()
    {
        $this->_forms['login'] = $this->_model->getForm('Login');
        return $this->_forms['login'];
    }

    public function getRegisterForm()
    {
        $this->_forms['register_form'] = $this->_model->getForm('Register');
        return $this->_forms['register_form'];
    }

    public function getForgotPasswordForm()
    {
        $this->_forms['forgotform'] = $this->_model->getForm('Forgot');
        return $this->_forms['forgotform'];
    }

}
?>
